package nju.mscs.gui;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.cloudgarden.layout.AnchorConstraint;
import nju.mscs.server.businessLogic.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class User extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JTabbedPane jTabbedPane1;
	private JPanel jPanel1;
	private JTable jTable3;
	private JButton jButton5;
	private JTextField jTextField7;
	private JLabel jLabel9;
	private JLabel jLabel8;
	private JButton jButton6;
	private JPanel jPanel3;
	private JButton jButton4;
	private JTable jTable2;
	private JButton jButton3;
	private JButton jButton2;
	private JTable jTable1;
	private JPanel jPanel2;
	private JButton jButton1;
	private JComboBox jComboBox1;
	private JLabel jLabel3;
	private JTextField jTextField2;
	private JLabel jLabel2;
	private JTextField jTextField1;
	private JLabel jLabel1;
	
	private DefaultTableModel jTable1Model;
    AccountManager am = new AM();
    SearchController sc = new SC();
	private int rowcount1=0;
	private int rowcount3=0;
	private JTextField jTextField6;
	private JLabel jLabel4;
	private JButton jButton8;
	private JButton jButton7;
	private JTextField jTextField3;
	private JLabel jLabel12;
	private JLabel jLabel7;
	private JComboBox jComboBox3;
	private JLabel jLabel11;
	private DefaultTableModel jTable3Model;
	private DefaultTableModel jTable2Model;
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				User inst = new User();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public User() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setUndecorated(true);
			{   
				jTabbedPane1 = new JTabbedPane();
				getContentPane().add(jTabbedPane1);
				jTabbedPane1.setBounds(28, 25, 1323, 719);
				jTabbedPane1.setFont(new java.awt.Font("微软雅黑",0,28));
				jTabbedPane1.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						jTabbedPane1MouseClicked(evt);
					}
				});
				{
					jPanel1 = new JPanel();
					jTabbedPane1.addTab("添加员工", null, jPanel1, null);
					jPanel1.setLayout(null);
					jPanel1.setPreferredSize(new java.awt.Dimension(463, 252));
					{
						jLabel1 = new JLabel();
						jPanel1.add(jLabel1);
						jLabel1.setText("\u65b0\u7528\u6237\u59d3\u540d");
						jLabel1.setBounds(28, 99, 144, 48);
						jLabel1.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jTextField1 = new JTextField();
						jPanel1.add(jTextField1);
						jTextField1.setBounds(215, 99, 293, 43);
						jTextField1.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jLabel2 = new JLabel();
						jPanel1.add(jLabel2);
						jLabel2.setText("\u65b0\u7528\u6237\u5bc6\u7801");
						jLabel2.setBounds(28, 193, 158, 52);
						jLabel2.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jTextField2 = new JTextField();
						jPanel1.add(jTextField2);
						jTextField2.setBounds(215, 203, 293, 42);
						jTextField2.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jLabel3 = new JLabel();
						jPanel1.add(jLabel3);
						jLabel3.setText("\u65b0\u7528\u6237\u804c\u4f4d");
						jLabel3.setBounds(694, 104, 167, 39);
						jLabel3.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						ComboBoxModel jComboBox1Model = 
							new DefaultComboBoxModel(
									new String[] { "收银员", "分店经理","总经理" });
						jComboBox1 = new JComboBox();
						jPanel1.add(jComboBox1);
						jComboBox1.setModel(jComboBox1Model);
						jComboBox1.setBounds(881, 103, 171, 40);
						jComboBox1.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jButton1 = new JButton();
						jPanel1.add(jButton1);
						jButton1.setText("\u6dfb\u52a0");
						jButton1.setBounds(714, 537, 155, 60);
						jButton1.setFont(new java.awt.Font("微软雅黑",0,26));
						jButton1.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton1ActionPerformed(evt);
							}
						});
					}
					{
						jTable1Model = 
							new DefaultTableModel(
									new String[][]{ },
									new String[] {"工号","姓名","职位","密码"});
						//jTable1.setModel(jTable1Model);
						jTable1 = new JTable(jTable1Model);
						jTable1.getTableHeader().setFont(new java.awt.Font("微软雅黑",0,28));
						jTable1.setFont(new java.awt.Font("微软雅黑",0,28));
						jTable1.setRowHeight(30);
						jPanel1.add(jTable1);
						JScrollPane scrollpane1 = new JScrollPane(jTable1);
						jPanel1.add(scrollpane1);
						scrollpane1.setBounds(28, 291, 1271, 213);
					}
					{
						jButton2 = new JButton();
						jPanel1.add(jButton2);
						jButton2.setText("\u8fd4\u56de");
						jButton2.setBounds(1003, 534, 161, 63);
						jButton2.setFont(new java.awt.Font("微软雅黑",0,28));
						jButton2.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton2ActionPerformed(evt);
							}
						});
					}
					{
						jLabel4 = new JLabel();
						jPanel1.add(jLabel4);
						jLabel4.setText("\u5206\u5e97\u53f7");
						jLabel4.setBounds(694, 195, 145, 48);
						jLabel4.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jTextField3 = new JTextField();
						jPanel1.add(jTextField3);
						jTextField3.setBounds(807, 204, 374, 39);
						jTextField3.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jButton7 = new JButton();
						jPanel1.add(jButton7);
						jButton7.setText("\u6e05\u9664");
						jButton7.setBounds(86, 537, 155, 59);
						jButton7.setFont(new java.awt.Font("微软雅黑",0,28));
						jButton7.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton7ActionPerformed(evt);
							}
						});
					}
				}
				{
					jPanel3 = new JPanel();
					jTabbedPane1.addTab("职位变化", null, jPanel3, null);
					jPanel3.setLayout(null);
					jPanel3.setPreferredSize(new java.awt.Dimension(474, 252));
					{
						jLabel8 = new JLabel();
						jPanel3.add(jLabel8);
						jLabel8.setText("\u5458\u5de5\u59d3\u540d");
						jLabel8.setBounds(658, 373, 112, 35);
						jLabel8.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jLabel9 = new JLabel();
						jPanel3.add(jLabel9);
						jLabel9.setText("\u5458\u5de5\u804c\u4f4d");
						jLabel9.setBounds(69, 448, 143, 41);
						jLabel9.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jTextField7 = new JTextField();
						jPanel3.add(jTextField7);
						jTextField7.setBounds(780, 377, 322, 37);
						jTextField7.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jButton5 = new JButton();
						jPanel3.add(jButton5);
						jButton5.setText("\u4fee\u6539/\u4fdd\u5b58");
						jButton5.setBounds(741, 574, 162, 56);
						jButton5.setFont(new java.awt.Font("微软雅黑",0,28));
						jButton5.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton5ActionPerformed(evt);
							}
						});
					}
					{
						jButton6 = new JButton();
						jPanel3.add(jButton6);
						jButton6.setText("\u8fd4\u56de");
						jButton6.setBounds(1053, 570, 158, 60);
						jButton6.setFont(new java.awt.Font("微软雅黑",0,28));
						jButton6.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton6ActionPerformed(evt);
							}
						});
					}
					{
						jLabel11 = new JLabel();
						jPanel3.add(jLabel11);
						jLabel11.setText("\u5458\u5de5\u5bc6\u7801");
						jLabel11.setBounds(658, 455, 123, 27);
						jLabel11.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						ComboBoxModel jComboBox3Model = 
							new DefaultComboBoxModel(
									new String[] { "收银员", "分店经理","总经理"});
						jComboBox3 = new JComboBox();
						jPanel3.add(jComboBox3);
						jComboBox3.setModel(jComboBox3Model);
						jComboBox3.setBounds(216, 451, 132, 34);
						jComboBox3.setFont(new java.awt.Font("微软雅黑",0,28));
					}
					{
						jTextField6 = new JTextField();
						jPanel3.add(jTextField6);
						jTextField6.setBounds(777, 449, 321, 40);
						jTextField6.setFont(new java.awt.Font("微软雅黑",0,28));
					}
				}
				{
					jPanel2 = new JPanel();
					jTabbedPane1.addTab("员工离职", null, jPanel2, null);
					jPanel2.setLayout(null);
					jPanel2.setPreferredSize(new java.awt.Dimension(461, 252));
					{
						jButton3 = new JButton();
						jPanel2.add(jButton3);
						jButton3.setText("\u79fb\u9664");
						jButton3.setBounds(716, 553, 162, 57);
						jButton3.setFont(new java.awt.Font("微软雅黑",0,28));
						jButton3.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton3ActionPerformed(evt);
							}
						});
					}
					{
						jTable2Model = 
							new DefaultTableModel(
									new String[][]{ },
									new String[] {"工号","姓名","职位"});
						jTable2 = new JTable(jTable2Model){
							public boolean isCellEditable(int row, int column) 
							{          if(column>-1){
								return false;}
							return true;
							}//表格不允许被编辑
						};				
						jPanel2.add(jTable2);
						JScrollPane scrollpane2 = new JScrollPane(jTable2);
						jPanel2.add(scrollpane2);
						jTable2.getTableHeader().setFont(new java.awt.Font("微软雅黑",0,28));
						jTable2.setFont(new java.awt.Font("微软雅黑",0,28));
						jTable2.setRowHeight(30);
						scrollpane2.setBounds(23, 42, 1275, 482);
						scrollpane2.setFont(new java.awt.Font("微软雅黑",0,28));
						String[][] userlist2 = sc.getUserList();
						int len = userlist2.length;
						for(int k=0;k<len;k++){
							jTable2Model.addRow(userlist2[k]);
						}
					}
					{
						jButton4 = new JButton();
						jPanel2.add(jButton4);
						jButton4.setText("\u8fd4\u56de");
						jButton4.setBounds(1003, 551, 166, 61);
						jButton4.setFont(new java.awt.Font("微软雅黑",0,28));
						jButton4.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton4ActionPerformed(evt);
							}
						});
					}
					{
						jButton8 = new JButton();
						jPanel2.add(jButton8);
						jButton8.setText("\u5237\u65b0");
						jButton8.setBounds(87, 551, 167, 61);
						jButton8.setFont(new java.awt.Font("微软雅黑",0,28));
						jButton8.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jButton8ActionPerformed(evt);
							}				});
					}
				}
			}
			{
				jTable3Model = 
					new DefaultTableModel(
							new String[][]{ },
							new String[] {"工号","姓名","职位","密码"});
				jTable3 = new JTable(jTable3Model){
					public void changeSelection(int rowIndex, int columnIndex,
			                boolean toggle, boolean extend){
			                super.changeSelection(rowIndex, columnIndex, toggle, extend);
			                super.editCellAt(rowIndex, columnIndex, null);}
		             public boolean isCellEditable(int row, int column) 
	                  {          if(column==0){
	                             return false;}
	                  return true;
	                  }//表格不允许被编辑
	                  };				
				jPanel3.add(jTable3);
				jTable3.getTableHeader().setFont(new java.awt.Font("微软雅黑",0,28));
				jTable3.setFont(new java.awt.Font("微软雅黑",0,28));
				jTable3.setRowHeight(30);
				JScrollPane scrollpane3 = new JScrollPane(jTable3);
				jPanel3.add(scrollpane3);
				scrollpane3.setBounds(22, 15, 1275, 303);
				scrollpane3.addMouseListener(new MouseAdapter() {
					public void mousePressed(MouseEvent evt) {
						scrollpane3MousePressed(evt);
					}
				});
				jTable3.getSelectionModel().addListSelectionListener(new ListSelectionListener(){    
		            public void valueChanged(ListSelectionEvent e)  
		            {  
		               int  selectrow = jTable3.getSelectedRow();
		               jLabel12.setText(jTable3.getValueAt(selectrow,0).toString());
		       		jTextField7.setText(jTable3.getValueAt(selectrow,1).toString());
		       		jTextField6.setText(jTable3.getValueAt(selectrow,3).toString());
		       		jComboBox3.setSelectedItem(jTable3.getValueAt(selectrow,2).toString());
		            }  
		        });  
				//rowcount3=sc.getUserList().length;
				//for(int rowcount=0;rowcount<rowcount3;rowcount++)
			    //jTable3Model.addRow(sc.getUserList()[rowcount]);
				
			}
			{
				jLabel7 = new JLabel();
				jPanel3.add(jLabel7);
				jLabel7.setText("\u5458\u5de5\u5de5\u53f7");
				jLabel7.setBounds(69, 373, 120, 35);
				jLabel7.setFont(new java.awt.Font("微软雅黑",0,28));
			}
			{
				jLabel12 = new JLabel();
				jPanel3.add(jLabel12);
				jLabel12.setBounds(204, 373, 175, 35);
				jLabel12.setFont(new java.awt.Font("微软雅黑",0,28));
			}
			pack();
			this.setSize(1393, 819);
			getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void jButton1ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton1.actionPerformed, event="+evt);
		String name1 = jTextField1.getText();
		String code1 = jTextField2.getText();
		String num1 = jTextField3.getText();//获取分店号
		System.out.println("Asd:"+name1);
		if(name1.trim().equals("")||code1.trim().equals("")){
			System.out.println("Asd");
			JOptionPane.showMessageDialog(this,"新员工数据不能为空！");
			return;
		}
		String job1 = jComboBox1.getSelectedItem().toString();
		//int jobcode1 = getJobCode(job1);
		
		String[] result1 = am.createNewUser(name1, code1, job1, num1);
		jTable1Model.addRow(result1);
		//rowcount1+=1;
		jTextField1.setText(null);
		jTextField2.setText(null);
	}//添加员工“添加”按钮事件
	
	private int getJobCode(String job1) {
		int jobcode=0;
		if(job1.equals("收银员"))jobcode=0;
		if(job1.equals("分店经理"))jobcode=1;
		if(job1.equals("经理"))jobcode=2;
		return jobcode;
	}

	private void jButton7ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton7.actionPerformed, event="+evt);
		if(rowcount1==0){
			JOptionPane.showMessageDialog(this,"已无员工可删除！");
			return;}
		{jTable1Model.removeRow(rowcount1-1);
		rowcount1--;}
		
	}//添加员工“清除”按钮事件
	
	
	
	private void jButton2ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton2.actionPerformed, event="+evt);
		login4_m.main(null);
		this.dispose();
	}//添加员工“返回”按钮事件
	
	private void jButton5ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton5.actionPerformed, event="+evt);
		String ID3 = jLabel12.getText();
		String name3 = jTextField7.getText();
		String code3 = jTextField6.getText();System.out.println(code3);
		String job3 = jComboBox3.getSelectedItem().toString();System.out.println(job3);
		if(name3.equals(null)||code3.equals(null)){
			JOptionPane.showMessageDialog(this,"员工信息不能为空！");
			return;
		}
		String[] changedinfo = {ID3,name3,code3,job3};
		boolean result3 = am.changeUser(changedinfo);
		jTable3Model.setRowCount(0);
			rowcount3=sc.getUserList().length;
			for(int rowcount=0;rowcount<rowcount3;rowcount++){
				jTable3Model.addRow(sc.getUserList()[rowcount]);
				}	
	    if(result3){
	    	JOptionPane.showMessageDialog(this,"上传成功！");
			return;
	    }
	    else{
	    	JOptionPane.showMessageDialog(this,"上传成功！");
			return;
	    }
	}//职位变动“修改/保存”按钮事件
	
	private void scrollpane3MousePressed(MouseEvent evt) {
		//System.out.println("scrollpane3.mouseClicked, event="+evt);
		int selectednum = jTable3.getSelectedRow();
		jLabel12.setText(jTable3.getValueAt(selectednum,0).toString());
		jTextField7.setText(jTable3.getValueAt(selectednum,1).toString());
		jTextField6.setText(jTable3.getValueAt(selectednum,3).toString());
		jComboBox3.setSelectedItem(jTable3.getValueAt(selectednum,2).toString());
	}	
	
	private void jTabbedPane1MouseClicked(MouseEvent evt) {
		//System.out.println("jTabbedPane1.mouseClicked, event="+evt);
		jTable3Model.setRowCount(0);
		rowcount3=sc.getUserList().length;
		for(int rowcount=0;rowcount<rowcount3;rowcount++){
			jTable3Model.addRow(sc.getUserList()[rowcount]);
			}	
	}
	
	private void jButton6ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton6.actionPerformed, event="+evt);
		login4_m.main(null);
		this.dispose();
	}//职位变动-返回
	
	private void jButton4ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton4.actionPerformed, event="+evt);
		login4_m.main(null);
		this.dispose();
	}//员工离职-返回
	
	private void jButton3ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton3.actionPerformed, event="+evt);
		int rowselect = jTable2.getSelectedRow();
		String deleteid = jTable2.getValueAt(rowselect, 0).toString();
		Boolean result2 = am.deleteUser(deleteid);
		if(result2){
			JOptionPane.showMessageDialog(this,"删除成功！");
			return;
		}
		else{
			JOptionPane.showMessageDialog(this,"删除失败！");
			return;
		}
		
		
	}//员工离职-修改
	
	private void jButton8ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton8.actionPerformed, event="+evt);
		jTable2Model.setRowCount(0);
		String[][] userlist2 = sc.getUserList();
		int len = userlist2.length;
		for(int k=0;k<len;k++){
			jTable2Model.addRow(userlist2[k]);
		}
	}//员工离职-刷新

}
